package com.xxx.server.controller;


import com.aliyun.oss.OSSClient;
import com.xxx.server.pojo.Admin;
import com.xxx.server.pojo.RespBean;
import com.xxx.server.service.IAdminService;
import com.xxx.server.utils.OSSUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private OSSUtil ossUtil;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/admin/info")
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication){
        if (adminService.updateById(admin)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,
                    null,authentication.getAuthorities()));
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "更新用户密码")
    @PutMapping("/admin/pass")
    public RespBean updateAdminPassword(@RequestBody Map<String,Object> info){
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updateAdminPassword(oldPass,pass,adminId);
    }

    @ApiOperation(value = "更新用户头像")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "头像", dataType = "MultipartFile")})
    @PostMapping("/admin/userface")
    public RespBean updateUserFace(MultipartFile file, Integer id, Authentication authentication){
        String url = ossUtil.getUrlAndUpload(file);
        return adminService.updateAdminUserFace(url,id,authentication);
    }
    // 未对异常进行处理
    @DeleteMapping(value = "/image/{imageName}")
    public void deleteImage(@PathVariable String imageName) {
        // EndPoint以上海为例
        String endPoint = "https://oss-cn-shanghai.aliyuncs.com";
        // 阿里云API秘钥
        String accessKeyId = "LTAI4G8TCZoqXj6GHYfk8Xid";
        String accessKeySecret = "KG1fz6SBFpsl30onXrx8erPMVBr5Mu";
        String bucketName = "dbdbc1d0b91f40e2b97ea77d491195b6";
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        client.deleteObject(bucketName, imageName);
        client.shutdown();
    }

    // 未对异常进行处理
    @DeleteMapping(value = "/image1/{imageName}")
    public RespBean deleteImage1(@PathVariable String imageName) {
        String delete = ossUtil.delete(imageName);
        return RespBean.success("更新", delete);
    }


}
